package com.feng.springboot01.controller;

import com.feng.springboot01.dao.DepartmentDao;
import com.feng.springboot01.dao.EmployeeDao;
import com.feng.springboot01.pojo.Department;
import com.feng.springboot01.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

@RequestMapping("/user")
@Configuration
public class EmployeeController {

    @Autowired
    DepartmentDao departmentDao;
    @Autowired
    EmployeeDao employeeDao;
    //去添加员工的页面
    @RequestMapping("/goAddEmployee")
    public String goAddEmployee( Model model){
        //添加部门信息 在前端可以读到部门信息
        Collection<Department> allDepartment = departmentDao.getAllDepartment();
        model.addAttribute("departs",allDepartment);
        return "/emp/add_emp";
    }
    //添加员工
    @RequestMapping("/addEmployee")
    public String addEmployee(Employee employee){
        employeeDao.addEmployee(employee);
        return "redirect:/employee.html";
    }

    //去往编辑员工的界面
    @RequestMapping("/toEditPage/{id}")
    public String edit(@PathVariable int id,Model model){
        //将要编辑的员工信息放入model
        model.addAttribute("emp",employeeDao.getById(id));
        model.addAttribute("departs",departmentDao.getAllDepartment());
        return "/emp/edit_emp";
    }
    //更新员工数据
    @PostMapping("/edit")
    public String goEdit(Employee employee){

        employeeDao.save(employee);
        return "forward:/employee.html";
    }

    //删除员工
    @RequestMapping("/del/{id}")
    public String goEdit(@PathVariable int id){
        employeeDao.deleEmployee(id);
            return "forward:/employee.html";
    }


}
